#!usr/bin/node

const Koa = require('koa'),
      Router = require('koa-router'),
      bodyparser = require('koa-bodyparser');

const app = new Koa(),
      router = new Router();

router.get('/api/authors/',async (ctx,next) => {
  const {page,limit} = ctx.query;
  const userAgent = ctx.header['user-agent'];
  console.log(`user-agent:${userAgent}`);
  console.log(`得到漫画书作者列表,page:${page},limit:${limit}`);
  ctx.body = 'get authors list ok';
});

router.get('/api/authors/:authorName',async (ctx,next) => {
  const {authorName} = ctx.params;
  console.log(`按作者名称查询:${authorName}`);
  ctx.body = 'get authors by name ok';
});

router.post('/api/authors/',async (ctx,next) => {
  const {authorName} = ctx.request.body;
  console.log(`添加新的漫画书作者信息:${authorName}`);
  ctx.body = 'insert ok';
});

router.put('/api/authors/:id',async (ctx,next) => {
  const {id} = ctx.params,
        {authorName} = ctx.request.body;
  console.log(`修改某id = ${id}的漫画书作者信息:${authorName}`);
  ctx.body = 'update ok';
});

router.delete('/api/authors/:id',async (ctx,next) => {
  const {id} = ctx.params;
  console.log(`删除某id = ${id}的漫画书作者信息`);
  ctx.body = 'delete ok';
});

app.use(bodyparser({enableTypes:['json','form','text']}));

app.use(router.routes());

app.listen(8080);
